from django.contrib.auth.models import User
from django.db import models


class Role(models.Model):
    """角色表"""
    name = models.CharField(max_length=64, unique=True, verbose_name='角色名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '角色表'
        verbose_name_plural = verbose_name


class UserProfile(models.Model):
    '''用户信息表'''
    # 关联django自带的User，可以自己扩展字段
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField('姓名', max_length=64)
    sex = models.CharField(choices=(('0', '男'), ('1', '女')), verbose_name='性别', max_length=2)
    avatar = models.ImageField(verbose_name='头像', upload_to='media/images/', blank=True)
    remark = models.CharField(max_length=256, verbose_name='备注', blank=True)
    # 一个用户可以有多个角色，一个角色可以对应多个用户
    role = models.ManyToManyField(Role, blank=True, null=True, verbose_name='角色')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '用户信息表'
        verbose_name_plural = verbose_name


class CustomerInfo(models.Model):
    '''客户信息表'''
    name = models.CharField('姓名', max_length=64, default=None)
    contact_type_choices = ((0, 'qq'), (1, '微信'), (2, '手机'))
    contact_type = models.SmallIntegerField(choices=contact_type_choices, default=0)
    contact = models.CharField('联系方式', max_length=64, unique=True)
    source_choices = ((0, '医院'), (1, '个人'), (2, '企业推荐'), (3, '网络自查'))
    source = models.SmallIntegerField('客户来源', choices=source_choices)
    status_choices = ((0, '正常'), (1, '停用'), (2, '弃用'))
    status = models.SmallIntegerField('客户状态', choices=status_choices)
    consultant = models.ForeignKey('UserProfile', verbose_name='客户顾问', on_delete=models.CASCADE)
    create_date = models.DateField('创建的时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '客户信息表'
        verbose_name_plural = verbose_name


class Hospital(models.Model):
    '''医院信息表'''
    customer = models.ForeignKey('CustomerInfo', verbose_name='客户', on_delete=models.CASCADE)
    name = models.CharField(max_length=256, verbose_name='医院名称')
    address = models.CharField(max_length=256, verbose_name='地址', blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '医院信息表'
        verbose_name_plural = verbose_name


